package jdbc;

import jdbc.common.InputUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class JDBCDemo10 {
    public static void main(String[] args) {
        UserInfo userInfo = InputUtil.getInputObject(new UserInfo());

        try (Connection connection = DBUtil.getConnection();) {
            String sql = "INSERT INTO userinfo(username,password,nickname,age) " +
                    "VALUES(?,?,?,?)";
            PreparedStatement st = connection.prepareStatement(sql);
            st.setString(1, userInfo.getUsername());
            st.setString(2, userInfo.getPassword());
            st.setString(3, userInfo.getNickname());
            st.setInt(4, userInfo.getAge());

            int count = st.executeUpdate();

            System.out.println(count > 0 ? "注册成功" : "注册失败");
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
